import Vue from 'vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue'
import router from './router'
import axios from 'axios'
import VueCoreVideoPlayer from 'vue-core-video-player'
import './assets/main.css'

Vue.use(VueCoreVideoPlayer)
Vue.use(ElementUI);
axios.defaults.withCredentials = true
//axios.defaults.baseURL='http://127.0.0.1:8080';
import cookies from 'vue-cookies'
Vue.prototype.$cookies = cookies;

router.beforeEach((to, from, next) => {
  /* 页面title */
  if (to.meta.title) {
    document.title = to.meta.title
  }
  if(to.matched.length === 0){
      from.path ? next({name: from.name}) : next('/404')
  }else{
      next()
  }
  /* 判断该路由是否需要登录权限 */
  if (to.matched.some(record => record.meta.requireAuth)) {
    //是否登录
    axios.get('api/account/login')
        .then(function (response) {
          if (response.data.responseCode != 200) {
            alert("您的token已失效，请先登录")
            next({
              path: '/',
            })
          }
          else {
              console.log('已自动登录')
            //已登录
          }
        })
        .catch(function (error) {
          // Toast(error.data.msg);
            alert("您的token已失效，请先登录")
            next({
                path: '/',
            })
        });
  }
  next();
})

new Vue({
  router,
  render: (h) => h(App)
}).$mount('#app')
